package com.erps.sys.rest;

import java.util.List;
import java.util.Map;

import com.erps.CommonUtils.BeanUtil;
import com.erps.CommonUtils.JActiveTable;
import com.erps.CommonUtils.StringEx;
import com.erps.CommonUtils.ValueType;
import com.erps.db.DBManager;
import com.hmkj.Utils.mail.MailUtils;
import com.hmkj.entity.ErspSysUser;

public class ErspSysUserRest {
	public int add(ErspSysUser vo) {
		String sql = BeanUtil.getInsertSQL("ersp_sys_user", vo);
		System.out.println(sql.toString());
		return DBManager.ExecSQL(sql);
	}

	public int update(ErspSysUser vo, String cWhereParm) {
		String sql = BeanUtil.getUpdateSQL("ersp_sys_user", vo, cWhereParm);
		System.out.println(sql.toString());
		return DBManager.ExecSQL(sql);
	}

	public ErspSysUser FindOne(String cKeyID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * ");
		sql.append(" from ersp_sys_user ");
		sql.append(" where id='" + cKeyID + "'");
		ErspSysUser vo = null;

		Map<String, String> rs = DBManager.GetRow(sql.toString());
		if (rs != null) {
			vo = new ErspSysUser();
			vo.setId(StringEx.getMapVal(rs, "id"));
			vo.setOrg_id(StringEx.getMapVal(rs, "org_id"));
			vo.setUser_passhash(StringEx.getMapVal(rs, "user_passhash"));
			vo.setUser_identity(StringEx.getMapVal(rs, "user_identity"));
			vo.setUser_phone(StringEx.getMapVal(rs, "user_Phone"));
			vo.setUser_status((short) StringEx.getInt(StringEx.getMapVal(rs, "user_status")));
			vo.setUser_idtype(StringEx.getInt(StringEx.getMapVal(rs, "user_idtype")));

			vo.setUser_checked_resp(StringEx.getMapVal(rs, "user_checked_resp"));
			vo.setUser_realname(StringEx.getMapVal(rs, "user_Realname"));
			vo.setUser_checked(StringEx.getMapVal(rs, "user_checked"));
			vo.setUser_mailaddr(StringEx.getMapVal(rs, "user_Mailaddr"));
			vo.setUser_cardinfo(StringEx.getMapVal(rs, "user_cardinfo"));
			vo.setSys_path(StringEx.getMapVal(rs, "sys_path"));

			return vo;
		} else {
			return null;
		}
	}

	public ErspSysUser LoginQuery(String cKeyID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select id ");
		sql.append(" from ersp_sys_user ");
		sql.append(" where user_mailaddr='" + cKeyID + "' or user_phone='" + cKeyID + "' or user_identity='" + cKeyID + "'");
		List<Map<String, String>> dtRows = DBManager.QueryData(sql.toString());
		if (dtRows == null)
			return null;
		if (dtRows.size() > 1)
			return null;
		else
			return FindOne(StringEx.getMapVal(dtRows.get(0), "id"));
	}

	public PageData Query(int iPageNO, int iPageSize, String cWhereParm) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) ");
		sql.append(" from ersp_sys_user ");
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(" where  " + cWhereParm);
		}
		// 添加Where条件

		String cOrderBy = " order by last_update_time desc ";

		int iRowCount = StringEx.getInt(DBManager.GetStrValue(sql.toString()));
		int iPageCount = (int) Math.ceil(iRowCount / (double) iPageSize);
		if (iPageNO < 0)
			iPageNO = 1;
		else if (iPageNO > iPageCount)
			iPageNO = iPageCount;

		sql.setLength(0);
		sql.append(" select * ");
		sql.append(" from ersp_sys_user ");
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(" where  " + cWhereParm);
		}

		ErspSysOrgRest rest = new ErspSysOrgRest();
		List<Map<String, String>> dtRows = DBManager.QueryData(sql.toString(), cOrderBy, iPageNO, iPageSize);
		PageData ret = new PageData();
		ret.setRowcount(iRowCount);
		ret.setPagecount(iPageCount);
		ret.setPageno(iPageNO);
		int iLength = (dtRows == null) ? 0 : dtRows.size();
		for (int i = 0; i < iLength; i++) {
			Map<String, String> vo = dtRows.get(i);
			String cOrgID = StringEx.getMapVal(vo, "org_id");
			vo.put("org_name", rest.FindOne(cOrgID).getOrg_name());
			ret.getKeylist().add(vo);
		}
		return ret;
	}

	public int Check(ErspSysUser vo, String cType, String cUserID) {
		if (vo != null) {
			String cResult = cType.equals("1") ? "审批通过" : "未通过";
			String cTypeID = cType.equals("1") ? "1" : "2";
			StringBuffer sql = new StringBuffer();
			JActiveTable aTable = new JActiveTable();
			aTable.setDBTableName("ersp_sys_user");
			aTable.addFieldValue("user_checked", cTypeID);
			aTable.addFieldValue("user_checked_resp", cResult);
			aTable.addFieldValue("id", vo.getId());
			aTable.setDbKeyFields("id");
			aTable.set__rowkeys(vo.getId());
			return DBManager.ExecSQL(aTable.getSQL());
		} else {
			return ValueType.FAILURE;
		}
	}

	public int SendMail(ErspSysUser vo, String cType) {
		if (vo != null) {
			String cResult = cType.equals("1") ? "审批通过" : "未通过";
			String cMailAddr = vo.getUser_mailaddr();
			String cSubject = "学区资源共享平台(中心)";
			String cContent = "您的注册申请，我们审核结果为：" + cResult;
			Boolean isSendFlag = MailUtils.send_mail(cMailAddr, cSubject, cContent);// 发送HTML格式邮件
			if (isSendFlag)
				return ValueType.SUCCESS;
			else
				return ValueType.FAILURE;
		} else {
			return ValueType.FAILURE;
		}
	}
}